package com.sky.controller.admin;

import com.sky.constant.RedisConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController("AdminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 菜品添加
     *
     * @param dishDTO
     */
    @ApiOperation("菜品添加")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品：{}", dishDTO);
        //删除缓存
        clearCache(RedisConstant.SHOP_CATEGORY_DISH+ dishDTO.getCategoryId());
        return dishService.insertDish(dishDTO);
    }

    /**
     * 菜品分页查询
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<Map<String,Object>> queryByPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        return dishService.queryByPage(dishPageQueryDTO);
    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
    @ApiOperation("菜品删除")
    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") String ids){
        log.info("菜品删除：{}",ids);
        //将ids转换成Integer的数组
        String[] str = ids.split(",");
        Integer[] integers = new Integer[str.length];
        for (int i = 0; i < str.length; i++) {
            integers[i] = Integer.valueOf(str[i]);
        }
        //删除缓存
        clearCache(RedisConstant.SHOP_CATEGORY_DISH+"*");
        //调用接口并返回
        return dishService.deleteByIds(integers);
    }

    /**
     * 菜品主键查询
     * @param id
     * @return
     */
    @ApiOperation("菜品主键查询")
    @GetMapping("/{id}")
    public Result<DishVO> queryById(@PathVariable("id") Integer id){
        log.info("菜品主键查询：{}",id);
        return dishService.queryById(id);
    }

    /**
     * 菜品修改
     * @param dishDTO
     * @return
     */
    @ApiOperation("菜品修改")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("菜品修改：{}",dishDTO);
        //删除缓存
        clearCache(RedisConstant.SHOP_CATEGORY_DISH+"*");
        return dishService.updateDish(dishDTO);
    }

    /**
     * 菜品状态修改
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("菜品状态修改")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status,
                               @RequestParam("id") String id){
        log.info("菜品状态修改：status:{},id:{}",status,id);
        //删除缓存
        clearCache(RedisConstant.SHOP_CATEGORY_DISH+"*");
        return dishService.updateStatus(status,id);
    }

    /**
     * 菜品分类查询
     * @param categoryId
     * @return
     */
    @ApiOperation("菜品分类查询")
    @GetMapping("/list")
    public Result<List<Dish>> queryList(@RequestParam("categoryId") Integer categoryId){
        log.info("菜品分类查询：{}",categoryId);
        return dishService.queryByCategoryId(categoryId);
    }

    private void clearCache(String patten){
        //获取所有的key
        Set keys = redisTemplate.keys(patten);
        //删除所有的key
        redisTemplate.delete(keys);
    }

}
